home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / map.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  3KB  |  112 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #ifndef _MAP_H
  30. #define _MAP_H
  31.  
  32. #include "pair.h"
  33. #include "ptrlist.h"
  34.  
  35. // this is a simple map from one data type to another
  36. // no dup checking is currently done... sorry :( --BU
  37.  
  38. #if 0
  39. template <class T> class SortPair {
  40. public:
  41.   static int compareItem(T *p1, T* p2) {
  42.     if (*p1 < *p2) return -1;
  43.     if (*p1 == *p2) return 0;
  44.     return 1;
  45.   }
  46.   static int compareAttrib(char *attrib, T *item) {
  47.     return compareItem((T*)attrib, item);
  48.   }
  49. };
  50. #endif
  51.  
  52. template <class INDEX, class DATA, class SORT=NotSorted>
  53. class Map {
  54. public:
  55.   typedef Pair<INDEX, DATA> MapPair;
  56.   Map() {
  57.     list.setAutoSort(0);    // for now, no sorting
  58.   }
  59.   ~Map() {
  60.     list.deleteAll();
  61.   }
  62.  
  63.   void addItem(const INDEX &a, const DATA &b) {
  64.     list.addItem(new MapPair(a, b));
  65.   }
  66.  
  67.   int getItem(const INDEX &a, DATA *b=NULL) {    // FUCKO: use a sorted ptrlist
  68.     //MapPair *mp = list.findItem((char *)&a);
  69.     MapPair *mp = NULL;
  70.     for (int i = 0; i < list.getNumItems(); i++) {
  71.       if (list[i]->a == a) {
  72.         mp = list[i];
  73.         if (b) *b = mp->b;
  74.         return 1;
  75.       }
  76.     }
  77.     return 0;
  78.   }
  79.  
  80.   int delItem(INDEX a) {
  81.     for (int i = 0; i < list.getNumItems(); i++) {
  82.       MapPair *pair = list[i];
  83.       if (pair->a == a) {
  84.         list.delByPos(i);
  85.         delete pair;
  86.         return 1;
  87.       }
  88.     }
  89.     return 0;
  90.   }
  91.  
  92.   int getNumItems() const {
  93.     return list.getNumItems();
  94.   }
  95.  
  96.   DATA enumItemByPos(int n, DATA default_val) {
  97.     MapPair *mp = list.enumItem(n);;
  98.     if (mp == NULL) return default_val;
  99.     return mp->b;
  100.   }
  101.  
  102.   int getNumPairs() const {
  103.     return list.getNumItems();
  104.   }
  105.  
  106. private:
  107.   PtrListQuickSorted<MapPair, SORT> list;
  108. //  PtrList<MapPair> list;
  109. };
  110.  
  111. #endif
  112.